<template>
  <div class="system-content">
    <w-form size="small" ref="form" label-width="100px" :options="options" :inline="true">
      <template #action="scope">
        <el-button type="primary" @click="submitForm(scope)" id="search">搜索</el-button>
        <el-button @click="resetForm">重 置</el-button>
        <el-button type="success" @click="handleAdd">添加</el-button>
        <el-button type="danger" @click="deleteMultiple">批量删除</el-button>
      </template>
    </w-form>
    <el-table height="500" :data="tableList" v-if="tableList.length" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" />
      <el-table-column type="index" label="序号" width="70" />
      <el-table-column v-for="item in tableHeadList" :prop="item.prop" :label="item.name" />
      <el-table-column align="right">
        <template #default="scope">
          <el-button type="success" size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="pt-4 page-wrap">
      <el-pagination
        v-model:current-page="currentPage"
        v-model:page-size="pageSize"
        :page-sizes="[10, 20, 50, 100]"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
    <el-dialog v-model="dialogFormVisible" title="添加产量产值">
      <w-form ref="form1" label-width="110px" :options="options1">
        <template #action="scope">
          <div class="text-end w-100">
            <el-button type="success" @click="save(scope)">确定</el-button>
          </div>
        </template>
      </w-form>
    </el-dialog>
  </div>
</template>

<script lang="ts" setup>
import { FormOptions, FormInstance } from '../components/form/src/types/types';
import { ref } from 'vue';
import mushroomout from '../api/mushroom';
import { ElMessage, ElMessageBox } from 'element-plus';
let form = ref();

interface Scope {
  form: FormInstance;
  model: any;
}

let options: FormOptions[] = [
  {
    type: 'input',
    value: '',
    label: '编号',
    prop: 'code',
    attrs: {
      clearable: true,
    },
  },
  {
    type: 'input',
    value: '',
    label: '所属厂房',
    prop: 'factoryBuildingCode',
    attrs: {
      clearable: true,
    },
  },
  {
    type: 'input',
    value: '',
    label: '状态',
    prop: 'status',
    attrs: {
      clearable: true,
    },
  },
  {
    type: 'input',
    value: '',
    label: '所属阶段',
    prop: 'stage',
    attrs: {
      clearable: true,
    },
  },
  {
    type: 'date-picker',
    value: '',
    label: '开始时间',
    prop: 'startTime',
    attrs: {
      clearable: true,
    },
  },
  {
    type: 'date-picker',
    value: '',
    label: '结束时间',
    prop: 'endTime',
    attrs: {
      clearable: true,
    },
  },
];
let options1: any = [
  {
    type: 'input',
    value: '',
    label: '编号',
    prop: 'code',
    attrs: {
      clearable: true,
    },
    rules: [
      {
        required: true,
        message: '编号不能为空',
        trigger: 'blur',
      },
    ],
  },

  {
    type: 'input',
    value: '',
    label: '所属厂房',
    prop: 'factoryBuildingCode',
    attrs: {
      clearable: true,
    },
    rules: [
      {
        required: true,
        message: '所属厂房不能为空',
        trigger: 'blur',
      },
    ],
  },
  {
    type: 'input',
    value: '',
    label: '状态',
    prop: 'status',
    attrs: {
      clearable: true,
    },
    rules: [
      {
        required: true,
        message: '状态不能为空',
        trigger: 'blur',
      },
    ],
  },
  {
    type: 'input',
    value: '',
    label: '所属阶段',
    prop: 'stage',
    attrs: {
      clearable: true,
    },
    rules: [
      {
        required: true,
        message: '所属阶段不能为空',
        trigger: 'blur',
      },
    ],
  },
];
options1 = ref(options1);
let tableHeadList = [
  {
    name: '编号',
    prop: 'code',
  },
  {
    name: '所属厂房',
    prop: 'factoryBuildingCode',
  },
  {
    name: '状态',
    prop: 'status',
  },
  {
    name: '所属阶段',
    prop: 'stage',
  },
  {
    name: '创建时间',
    prop: 'createTime',
  },
];
let tableList = ref<any>([]);
let dialogFormVisible = ref(false);
let isAdd = ref(true);
let editRow = ref<any>({});
let ids = ref('');
let submitForm = (scope: Scope) => {
  scope.form.validate(async (valid) => {
    if (valid) {
      getTableData(scope);
    } else {
      // ElMessage.error('表单填写错误');
    }
  });
};
async function getTableData(scope = {}) {
  let model = (scope as any).model;
  if (model == null) {
    model = {};
  }
  let data = await mushroomout.mushroomoutList({ current: currentPage.value, size: pageSize.value, ...model });
  tableList.value = data.data.data.records;
  total.value = data.data.data.total;
}
let resetForm = () => {
  form.value.resetFields();
  getTableData();
};
const handleEdit = (index, row) => {
  isAdd.value = false;
  dialogFormVisible.value = true;
  editRow.val = row;
  options1.value.forEach((item) => {
    item.value = row[item.prop];
  });
};
const handleAdd = () => {
  isAdd.value = true;
  options1.value.forEach((item) => {
    item.value = '';
  });
  dialogFormVisible.value = true;
};
const deleteMultiple = (done: Function) => {
  ElMessageBox.confirm('你确定要删除吗')
    .then(async () => {
      let data: any = await mushroomout.mushroomoutDel(ids.value);
      if (data.isSuccess) {
        ElMessage.success('删除成功');
      }
      resetForm();
      done();
    })
    .catch(() => {
      // catch error
    });
};
const save = async (scope: Scope) => {
  scope.form.validate(async (valid) => {
    let data: any;
    if (valid) {
      if (isAdd.value) {
        data = await mushroomout.mushroomoutAdd(scope.model);
        if (data.isSuccess) {
          ElMessage.success('添加成功');
        }
      } else {
        mushroomout.mushroomoutUpdate({
          ...scope.model,
          id: editRow.value.id,
          createTime: editRow.value.createTime,
        });
        if (data.isSuccess) {
          ElMessage.success('更新成功');
        }
      }
      dialogFormVisible.value = false;
      resetForm();
    } else {
      // ElMessage.error('表单填写错误');
    }
  });
};

const handleSelectionChange = (val: any) => {
  let value = '';
  val.forEach((item) => {
    value += item.id + ',';
  });
  ids.value = value.substring(0, value.length - 1);
};
let currentPage = ref(1);
let pageSize = ref(10);
let total = ref(0);
const handleSizeChange = (val) => {
  pageSize.value = val;
  currentPage.value = 1;
  const searchDom = document.getElementById('search');
  searchDom?.click();
};
const handleCurrentChange = (val) => {
  currentPage.value = val;
  const searchDom = document.getElementById('search');
  searchDom?.click();
};
getTableData();
</script>

<style lang="" scoped></style>
